﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

/// <summary>
/// Summary description for OutputHtml
/// </summary>
public class OutputHtml
{

    static String chBaseURL = "http://chart.apis.google.com/chart?";
    static HelperGoogleChart googleChart = new HelperGoogleChart();
    
    static public void RunExecutedProjectToHtml( HttpResponse response, String projectFileName, Data.Data analysisProject )
    {
        if( projectFileName != null )
            response.Write( "<br /><label class='banner'>" + projectFileName + "</label>" );

        for( int currentVisiblePageIndex = 0,  visiblePageCount = analysisProject.GetPageCount() ;
                currentVisiblePageIndex < visiblePageCount ;
                currentVisiblePageIndex++ )
        {
            analysisProject.SetSelectedPage( currentVisiblePageIndex );
            Data.Page currentPage = analysisProject.GetSelectedPage();
            if( currentPage.Visible() )
            {
                response.Write( "<br /><label class='PageCaption'>" + currentPage.GetPageCaption() + "</label>" );
                response.Write( "<br /><label class='PageSubCaption'>" + currentPage.GetPageDescription() + "</label>" );
                response.Write( "<br /><label class='PageSubCaption'>Total count: " + currentPage.GetRowCount() + "</label><br />" );

                String wantTable = currentPage.GetParameter( "wantTable" );
                String wantColumnCharts = currentPage.GetParameter( "wantColumnCharts" );
                String wantRowCharts = currentPage.GetParameter( "wantRowCharts" );
                Boolean anyChart = wantColumnCharts.Length > 0 || wantRowCharts.Length > 0;
                
                if( wantTable.Length > 0 || anyChart == false )
                {
                    response.Write( "<br />" );
                    OutputHtml.GenTableToPrintWriter( response, currentPage );
                }
                
                if( wantColumnCharts.Length > 0 )
                {
                    response.Write( "<br />" );
                    OutputHtml.getPieChartsSelectedColumns( response, currentPage, wantColumnCharts );
                }
                
                if( wantRowCharts.Length > 0 )
                {
                    response.Write( "<br />" );
                    OutputHtml.getRowCharts(response, currentPage, wantRowCharts);
                }
            }
        }
    }
    
    static private void GenTableToPrintWriter( HttpResponse response, Data.Page currentPage )
    {
        
        response.Write( "<br />" );
        response.Write( "<table class='dataTable'  >" );

        response.Write( "<tr>" );
        response.Write( "<td class='RowLabelCaption'>" );
        response.Write( currentPage.GetRowLabelName() );
        response.Write( "</td>" );

        for( int textColumnIndex = 0,  textCount = currentPage.GetTextColumnCount() ;
                textColumnIndex < textCount ; textColumnIndex++ )
        {
            response.Write( "<td class='TextColumnCaption'>" );
            response.Write( currentPage.GetTextColumnName( textColumnIndex ) );
            response.Write( "</td>" );
        }

        for( int numericColumnIndex = 0,  columnCount = currentPage.GetNumericColumnCount() ;
                numericColumnIndex < columnCount ; numericColumnIndex++ )
        {

            response.Write( "<td class='ColCaption'>" );
            response.Write( currentPage.GetNumericColumnName(numericColumnIndex ) );
            response.Write( "</td>" );
        }
        response.Write( "</tr>" );

        for( int rowIndex = 0,  rowCount = currentPage.GetRowCount() ;
            rowIndex < 10 &&
                rowIndex < rowCount ; rowIndex++ )
        {
            response.Write( "<tr>" );
            response.Write( "<td class='RowLabel'>" );
            response.Write( currentPage.GetRowLabelValue( rowIndex ) + "<br />" );
            response.Write( "</td>" );

            for( int textColumnIndex = 0,  textCount = currentPage.GetTextColumnCount() ;
                    textColumnIndex < textCount ; textColumnIndex++ )
            {
                response.Write( "<td class='TextColumn'>" );
                response.Write( currentPage.GetTextColumnValue( rowIndex, textColumnIndex ) );
                response.Write( "</td>" );
            }

            for( int numericColumnIndex = 0,  columnCount = currentPage.GetNumericColumnCount() ;
                    numericColumnIndex < columnCount ; numericColumnIndex++ )
            {
                String alertLevel = currentPage.GetAlertLevel( rowIndex, numericColumnIndex );

                String cssClass ;
                if( alertLevel.Equals( "-1") == false )
                    cssClass = "class='Alert" + alertLevel + "'";
                else
                    cssClass = "class='NormalDataCell'";
                

                response.Write( "<td " + cssClass + " >" );
                response.Write( currentPage.GetNumericCellContents( rowIndex, numericColumnIndex, true ) );
                response.Write( "</td>" );
            }
            response.Write( "</tr>" );
        }
        response.Write( "</table>" );
        
    }
    static private void getPieChartsSelectedColumns( HttpResponse response, Data.Page currentPage, String googleSpecs )
    {
        String sampleSpec  = "cht=p3&chf=c,lg,45,c0c0c0,0,ffffff,0.75|bg,s,ffffff&chs=600x300";
        
        for( int numericColumnIndex = 0,  columnCount = currentPage.GetNumericColumnCount() ;
                numericColumnIndex < columnCount ; numericColumnIndex++ )
        {
            if( currentPage.GetSelectedPosition("column", numericColumnIndex) )
            {
                // the slices are the the rows. The pie chart summarizes the data down a column.
                // the chart subtitle is the column caption.
                // the data are the cell values in a column
                // the labels are the row-labels
                String chTitle = googleChart.GetChartTitle( currentPage, true, numericColumnIndex, true, true );
                String chData = googleChart.GetData( currentPage, true, numericColumnIndex );
                String chPieMarkers = googleChart.GetPieLabelsRows(currentPage, numericColumnIndex );

                String chWholeThing = chBaseURL + "&" + chData + "&" + chPieMarkers + "&" + chTitle ;
                
                if( googleSpecs == null )
                   chWholeThing += "&" + sampleSpec;
                else
                   chWholeThing += "&" + googleSpecs; 

                response.Write( "<br /><img alt='chart' src='" + chWholeThing + "' />" );
            }
        }
    }
    
    static private void getRowCharts( HttpResponse response, Data.Page currentPage, String googleSpecs )
    {
        String sampleSpec  = "cht=lc&chf=c,lg,45,c0c0c0,0,ffffff,0.75|bg,s,ffffff&chs=600x300";

        for( int rowIndex = 0,  rowCount = currentPage.GetRowCount() ;
                rowIndex < rowCount && rowIndex < 10 ; rowIndex++ )
        {
            String chTitle = googleChart.GetChartTitle( currentPage, false, rowIndex, false, true );
            String chData= googleChart.GetData( currentPage, false, rowIndex ); 
            
            String chWholeThing = chBaseURL + "&" + chData + "&" + chTitle ;

            if( googleSpecs == null )
               chWholeThing += "&" + sampleSpec;
            else
               chWholeThing += "&" + googleSpecs; 

            response.Write( "<br /><img alt='chart' src='" + chWholeThing  + "' />" );
        }
    }
    
    static private void getMultiColumn( HttpResponse response, Data.Page currentPage, String googleSpecs )
    {
        response.Write( "<br />" );

        String sampleSpec  = "cht=lc&chf=c,lg,45,c0c0c0,0,ffffff,0.75|bg,s,ffffff&chs=600x300";

        String chData = googleChart.GetDataColumns( currentPage );
        String chLegend = googleChart.GetDataColumnNames(currentPage);

        String chWholeThing = chBaseURL 
                        + "&" + chData
                        + "&" + chLegend
                        + "&" + googleChart.GetDataColumnsColors( currentPage );
        
        if( googleSpecs == null )
           chWholeThing += "&" + sampleSpec;
        else
           chWholeThing += "&" + googleSpecs; 

        response.Write( "<br /><img alt='chart' src='" + chWholeThing  + "' />" );

    }
    static private void getMultiRow(HttpResponse response, Data.Page currentPage, String googleSpecs)
    {
        response.Write( "<br />" );
            
        String sampleSpec  = "cht=lc&chf=c,lg,45,c0c0c0,0,ffffff,0.75|bg,s,ffffff&chs=600x300";

        String chData = googleChart.GetDataAllRows( currentPage );
        String chLegend = googleChart.GetDataRowNames( currentPage );

        String chWholeThing = chBaseURL 
                        + "&" + chData
                        + "&" + chLegend
                        + "&" + googleChart.GetDataRowsColors( currentPage );
        
        if( googleSpecs == null )
           chWholeThing += "&" + sampleSpec;
        else
           chWholeThing += "&" + googleSpecs; 

        response.Write( "<br /><img alt='chart' src='" + chWholeThing  + "' />" );
    }
}
